﻿using System.Collections.Generic;
using System.Text;
using Jayden.Dll.Web.Enums;

namespace Jayden.Dll.Web.Json
{
	public class JsonArray : List<JsonValue>, JsonValue
	{
		public JsonArray() { }
		public JsonArray(IEnumerable<string> values)
		{
			foreach (string value in values)
				Add(value);
		}

		public void Add(string value) { base.Add(value == null ? null : new JsonString(value)); }

		public string ToJson(RenderMode mode, int level)
		{
			StringBuilder builder = new StringBuilder();
			foreach (JsonValue value in this)
			{
				if (builder.Length > 0)
					builder.Append(',');
				if (mode == RenderMode.Debug)
				{
					builder.AppendLine();
					for (int c = 0; c < level; c++)
						builder.Append("\t");
				}
				if (value == null)
					builder.Append(JsonHelper.Null);
				else
					builder.Append(value.ToJson(mode, level + 1));
			}
			if (mode == RenderMode.Debug && builder.Length > 0)
				builder.AppendLine();
			return "[" + builder.ToString() + "]";
		}
		public override string ToString() { return ToJson(RenderMode.Debug, 0); }
	}
}
